﻿using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using Sc.Domain;
using Sc.Service;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace Sc.Web.Controllers
{
    public class SendCarController : BaseController
    {
        /// <summary>
        /// 用车申请服务层
        /// </summary>
        private readonly Car_UseCarApplicationService service = new Car_UseCarApplicationService();
        /// <summary>
        /// 派车纪录服务层
        /// </summary>
        private readonly SendCarRecordService record = new SendCarRecordService();
        /// <summary>
        /// 派车
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            ViewData["cartype"] = new BaseItemService().GetListByType(5);
            return View();
        }
        /// <summary>
        /// 获取可以派车的列表
        /// </summary>
        /// <returns></returns>
        public JsonResult GetPageData()
        {
            var data = service.GetCarUseList(UserLogin.UserId, UserLogin.OrgzNo);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 消息数量
        /// </summary>
        /// <returns></returns>
        public string GetMsgCount()
        {
            int carusecount = service.GetCarUseList(UserLogin.UserId, UserLogin.OrgzNo).Count;
            int todocount = new Sys_ToDoListService().GetNoReadMsgCount(UserLogin.UserId);
            string json = "{\"UseCarCount\":" + carusecount + ",\"TodoCount\":" + todocount + "}";
            return json;
        }

        /// <summary>
        /// 保存派车纪录
        /// </summary>
        /// <param name="data">页面数据</param>
        /// <returns></returns>
        public int SaveData(string data)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            IList<SendCarRecord> list = json.Deserialize<IList<SendCarRecord>>(data);
            foreach (var item in list)
            {
                item.ScUserId = UserLogin.UserId;
            }
            return record.SaveData(list);
        }
        /// <summary>
        /// 派车纪录
        /// </summary>
        /// <returns></returns>
        public ActionResult SendCarRecord()
        {
            ViewData["type"] = new BaseItemService().GetListByType(5);
            return View();
        }

        /// <summary>
        /// 派车单
        /// </summary>
        /// <returns></returns>
        public ActionResult Paichedan()
        {
            string id = Request.Params["id"];
            string carno = Request.Params["carno"];
            if (string.IsNullOrEmpty(id))
            {
                SendCarRecordService service1 = new SendCarRecordService();
                var list = service1.GetSendCarRecordByCar(carno);
                if (list.Count > 0)
                {
                    ViewData["id"] = list[0].Id;
                }
                else
                    ViewData["id"] = 0;
            }
            else
                ViewData["id"] = id;
            return View();
        }
        /// <summary>
        /// 获取派车纪录
        /// </summary>
        /// <param name="pageSize">每页显示条数</param>
        /// <param name="pageIndex">当前页</param>
        /// <returns></returns>
        public JsonResult GetSendCarRecordData(int pageSize, int pageIndex)
        {
            var uName = Request.Params["uName"];
            var start = Request.Params["start"];
            var end = Request.Params["end"];
            var carNo = Request.Params["carNo"];
            var carType = Request.Params["carType"];
            var data = record.GetList(UserLogin.OrgzNo, pageSize, pageIndex, uName, start, end, carNo, carType);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取单条派车记录
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetSendCarRecord(int id)
        {
            var data = record.GetSendCarRecordById(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public JsonResult GetSendCarRecordForPrintById(int id)
        {
            var data = record.GetSendCarRecordForPrintById(id);
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 数据导出
        /// </summary>
        /// <returns></returns>
        public FileResult ExportData()
        {
            var dt = DateTime.Now;
            var st = Convert.ToDateTime(dt.AddMonths(-dt.Month + 1).AddDays(-dt.Day).ToString("yyyy-MM-dd"));
            var ed = st.AddYears(1).AddDays(1);
            var data = record.GetList(UserLogin.OrgzNo, 10000, 1, string.Empty, st.ToString("yyyy-MM-dd"), ed.ToString("yyyy-MM-dd"), string.Empty, string.Empty);
            HSSFWorkbook book = new HSSFWorkbook();
            ISheet sheet = book.CreateSheet("派车纪录");
            IRow row = sheet.CreateRow(0);//表头
            row.CreateCell(0).SetCellValue("编号");
            row.CreateCell(1).SetCellValue("用车人");
            row.CreateCell(2).SetCellValue("所在部门");
            row.CreateCell(3).SetCellValue("起点");
            row.CreateCell(4).SetCellValue("终点");
            row.CreateCell(5).SetCellValue("用车时间");
            row.CreateCell(6).SetCellValue("人数");
            row.CreateCell(7).SetCellValue("所派车辆");
            row.CreateCell(8).SetCellValue("车辆类型");
            row.CreateCell(9).SetCellValue("驾驶员");
            for (int i = 0; i < data.Data.Count; i++)
            {
                var item = data.Data[i];
                IRow temp = sheet.CreateRow(i + 1);
                temp.CreateCell(0).SetCellValue((i + 1).ToString());
                temp.CreateCell(1).SetCellValue(item.UserName);
                temp.CreateCell(2).SetCellValue(item.Orgz);
                temp.CreateCell(3).SetCellValue(item.StartAddress);
                temp.CreateCell(4).SetCellValue(item.EndAddress);
                temp.CreateCell(5).SetCellValue(item.StartTime.ToString("yyyy-MM-dd HH:mm:ss"));
                temp.CreateCell(6).SetCellValue(item.TotalCount);
                temp.CreateCell(7).SetCellValue(item.CarNo);
                temp.CreateCell(8).SetCellValue(item.CarTypeName);
                temp.CreateCell(9).SetCellValue(item.DriverName);
            }
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            book.Write(ms);
            ms.Seek(0, SeekOrigin.Begin);
            string dateTime = DateTime.Now.ToString("yyMMddHHmmssfff");
            string fileName = "派车纪录" + dateTime + ".xls";
            return File(ms, "application/vnd.ms-excel", fileName);
        }

        /// <summary>
        /// 保存还车
        /// </summary>
        /// <returns></returns>
        public int SaveReturnCar(int bid)
        {
            SendCarRecordService service1 = new SendCarRecordService();
            var data = service1.SaveReturnCar(bid);
            return data.SourceId;
        }


        /// <summary>
        /// 归还该车的派车信息
        /// </summary>
        /// <param name="cid"></param>
        /// <returns></returns>
        public int SaveReturnCarByCar(string carno)
        {
            SendCarRecordService service1 = new SendCarRecordService();
            var list = service1.GetSendCarRecordByCar(carno);
            if (list.Count > 0)
            {
                var data = service1.SaveReturnCar(list[0].Id);
                return data.SourceId;
            }
            return 0;
        }
        /// <summary>
        /// 拒绝派车
        /// </summary>
        /// <param name="id">用车申请ID</param>
        /// <param name="opition">拒绝内容</param>
        /// <returns></returns>
        public int RefuseSendCar(int id, string opition)
        {
            return service.RefuseSendCar(id, opition, UserLogin.UserId);
        }
    }
}